package com.sym.nestscrollview;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.FloatRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * @author : YOULU  sym
 * date   : 2021/3/8 0008
 * desc   : 自定义圆形进度条
 */
public class CircleProgressView extends View {

    /**
     * 画笔
     */
    private Paint mPaint;
    /**
     * 圆的宽度
     */
    private int mWidth;
    /**
     * 圆的高度
     */
    private int mHeight;

    /**
     * 渐变颜色范围
     */
    private final int[] mColors = {0xffFF8B73, 0xffFF3334};

    private int mStrokeWidth;

    public CircleProgressView(Context context) {
        super(context);
    }

    public CircleProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
        init();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.reset();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL);

        mPaint.reset();
        mPaint.setAntiAlias(true);
        mPaint.setColor(PICK_COlOR);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mRedCircleStrokeWidth);
        float radius = (mWidth - mRedCircleStrokeWidth) / 2;
        canvas.drawCircle(mCx, mCy, radius, mPaint);


        //渐变颜色 你可以添加很多种但是至少要保持在2种颜色以上
        float[] postions = {0f, mProgress};
        //circleWidth 圆的直径 取中心点
        SweepGradient sweepGradient = new SweepGradient(mCx, mCy, mColors, postions);
        sweepGradient.setLocalMatrix(mMatrix);

        mPaint.reset();
        mPaint.setAntiAlias(true);
        mPaint.setShader(sweepGradient);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(mRedCircleStrokeWidth);
        canvas.drawArc(mRectF, 90, 360 * mProgress, false, mPaint);

        double angle = Math.PI / 2;
        float x = (float) Math.cos(angle) * radius + mCx;
        float y = (float) Math.sin(angle) * radius + mCy;

        mPaint.reset();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mWhiteCircleStrokeWidth);
        mPaint.setColor(Color.WHITE);
        canvas.drawCircle(x, y, mRadiusWhiteCircle, mPaint);

        int bitmapWidth = mInnerCircleBitmap.getWidth();
        int bitmapHeight = mInnerCircleBitmap.getHeight();
        int left = (mWidth - bitmapWidth) / 2;
        int top = (mHeight - bitmapHeight) / 2;
        canvas.drawBitmap(mInnerCircleBitmap, left, top, null);
    }


    /**
     * 白色起始原半径
     */
    private float mRadiusWhiteCircle;

    /**
     * 白色圆圈的宽度
     */
    private float mWhiteCircleStrokeWidth;

    /**
     * 红色圆圈半径
     */
    private float mRadiusRedCircle;


    /**
     * 红色圆圈的宽度
     */
    private float mRedCircleStrokeWidth;

    /**
     * 粉色圆圈半径
     */
    private float mRadiusPinkCircle;

    /**
     * 粉色圆圈的宽度
     */
    private float mPinkCircleStrokeWidth;

    /**
     * 内圆圈半径
     */
    private float mRadiusInnerCircle;

    private Bitmap mInnerCircleBitmap;

    /**
     * x 轴坐标原点
     */
    private float mCx;
    /**
     * y 轴做坐标原点
     */
    private float mCy;

    /**
     * 进度
     */
    private float mProgress;

    /**
     * 矩阵
     */
    private Matrix mMatrix;

    /**
     * 矩形
     */
    private RectF mRectF;

    private final int PICK_COlOR = Color.parseColor("#80FCADAD");


    private void init() {
        mPaint = new Paint();

        Resources resources = getContext().getResources();
        mInnerCircleBitmap = BitmapFactory.decodeResource(resources, R.mipmap.inner_circle);

        mRadiusWhiteCircle = dip2px(getContext().getApplicationContext(), 3.5f);
        mRadiusInnerCircle = dip2px(getContext().getApplicationContext(), 44.5f);
        mRadiusPinkCircle = dip2px(getContext().getApplicationContext(), 55.5f);
        mRadiusRedCircle = dip2px(getContext().getApplicationContext(), 54.5f);

        mRedCircleStrokeWidth = dip2px(getContext().getApplicationContext(), 4.0f);
        mPinkCircleStrokeWidth = dip2px(getContext().getApplicationContext(), 4.0f);
        mWhiteCircleStrokeWidth = dip2px(getContext().getApplicationContext(), 2);

        mWidth = (int) (mWidth - mWhiteCircleStrokeWidth);
        mHeight = (int) (mHeight - mWhiteCircleStrokeWidth);

        mCx = mWidth / 2.0f;
        mCy = mHeight / 2.0f;

        mMatrix = new Matrix();
        mMatrix.setRotate(90, mCx, mCy);

        float tmp = mRedCircleStrokeWidth / 2;
        mRectF = new RectF(tmp, tmp, mWidth - tmp, mHeight - tmp);

    }


    /**
     * 设置进度
     *
     * @param progress 进度值
     */
    public void setProgress(@FloatRange(from = 0.0f, to = 1.0f) float progress) {
        this.mProgress = progress;
        invalidate();
    }

    /**
     * dp 转 px
     *
     * @param context {@link Context}
     * @param dpValue {@code dpValue}
     * @return {@code pxValue}
     */
    private static int dip2px(@NonNull Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
